package com.vancouver.knows.ui.activity.login;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.OnClick;

import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.activity.my.MyFeedbackAboutProcotolActivity;
import com.vancouver.knows.ui.base.BaseActivity;
import com.vancouver.knows.ui.common.MyAutoCompleteTextView;
import com.vancouver.knows.util.PhoConstants;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.task.mark.PhoneSendSecurityCodeTaskMark;

public class PhoneSecurityCodeActivity extends BaseActivity implements IResultReceiver{

	private Context mContext;
	@Bind(R.id.common_tips)
	TextView  mCommonTips;
	@Bind(R.id.common_back)
	ImageView mCommonBack;
	@Bind(R.id.email_register)
	TextView mEmailRegister;
	@Bind(R.id.get_code)
	Button mGetCode;
	@Bind(R.id.area)
	TextView mAreaTxt;
	@Bind(R.id.code)
	TextView mAreaCode;
	@Bind(R.id.phone)
	MyAutoCompleteTextView mPhoneEdt;
	@Bind(R.id.user_agreement)
	TextView userAgreement;
	
	private LoadingDialog loadingDialog;
	private String[] mAttrTitles;
	private String mPhoneAreaCode;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_phone_get_code);
		mContext = PhoneSecurityCodeActivity.this;
		setViews();
		setListeners();
	}

	private void setViews() {
		loadingDialog = new LoadingDialog(mContext);
		Intent intent = getIntent();
		String tips = intent.getStringExtra("COMMON_TIPS");
		mCommonTips.setText(tips);
		registerPhoneBroastcast();
	}
	
	private void setListeners(){
		mCommonBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	@OnClick(R.id.user_agreement)
	public void onUserAgreementClick(){
		mAttrTitles = mContext.getResources().getStringArray(R.array.my_list_titles);
		Intent intent = new Intent(mContext, MyFeedbackAboutProcotolActivity.class);
		if (mAttrTitles != null)
			intent.putExtra("USER_NOTICE", mAttrTitles[6]);
		intent.putExtra("PAGE_TYPE", "user_terms");
		startActivity(intent);
	}
	
	@OnClick(R.id.area)
	public void onAreaCountryClick() {
		Intent intent = new Intent(mContext,  ChooseCountryActivity.class);
		intent.putExtra("COMMON_TIPS", mContext.getString(R.string.choose_country).toString());
		intent.putExtra("CURRENT_OPEN_ACTIVITY", PhoConstants.CURRENT_OPEN_FROM_REGISTER_PHONE);
		mContext.startActivity(intent);
	}
	
	@OnClick(R.id.get_code)
	public void onGetCodeClick() {
		if (mPhoneEdt.getText().toString().trim().equals("")){
			Toast.makeText(mContext, mContext.getString(R.string.phone_format_tips), Toast.LENGTH_SHORT).show();
			return;
		}
		BusinessModule bModule = imContext.getBusinessModule();
		PhoneSendSecurityCodeTaskMark taskMark = bModule.getTaskMarkPool().getPhoneSendSecurityCodeTaskMarkList();
		String code = getAreaCode();
		mPhoneAreaCode = code;
		//发送登录需要的参数
		bModule.getServiceWrapper().userPhoneSendSecurityCode(PhoneSecurityCodeActivity.this, taskMark, code + mPhoneEdt.getText().toString().trim());
		if (loadingDialog != null)
			loadingDialog.showLoadingDialog(mContext.getString(R.string.send_code_loading));
	}
	
	private String getAreaCode(){
		String code = mAreaCode.getText().toString();
		while (code.contains("0")) {
			int begin = code.indexOf("0");
			if (begin != -1)
				code = code.substring(begin+1);
		}
		
		return code;
	}
	
	@OnClick(R.id.email_register)
	public void onEmailRegisterClick() {
		Intent intent = new Intent(mContext, EmailSecurityCodeActivity.class);
		intent.putExtra("COMMON_TIPS", mContext.getString(R.string.email_register).toString());
		mContext.startActivity(intent);
	}
	 
	private void registerPhoneBroastcast(){
		IntentFilter intentFilter = new IntentFilter(); 
		intentFilter.addAction(PhoConstants.CHOOSE_COUNTRY_INFO_REGISTER_PHONE);  
        this.registerReceiver(PhoneReceiver, intentFilter);
	}
	
	BroadcastReceiver PhoneReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(PhoConstants.CHOOSE_COUNTRY_INFO_REGISTER_PHONE)) {
				mAreaCode.setText(intent.getStringExtra("AREA_CODE"));	
				mAreaTxt.setText(intent.getStringExtra("COUNTRY"));	
			}
		}
	};
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (loadingDialog != null){
			loadingDialog.dismiss();
		}
	}

	@Override
	public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
		if (loadingDialog != null)
			loadingDialog.closeLoadingDialog();
		if (exception != null && !exception.getExMessage().equals("")){
			Toast.makeText(mContext, exception.getExMessage(), Toast.LENGTH_SHORT).show();
		}else{
			Intent intent = new Intent(mContext,  RegisterActivity.class);
			intent.putExtra("COMMON_TIPS", mContext.getString(R.string.phone_register).toString());
			intent.putExtra("REGISTER_COUNT", mPhoneAreaCode + mPhoneEdt.getText().toString().trim());
			intent.putExtra("AREA_CODE",mPhoneAreaCode);
			intent.putExtra("FROM_JUMP",  PhoConstants.CURRENT_OPEN_FROM_REGISTER_PHONE);
			mContext.startActivity(intent);
			finish();
		}
	}
}